﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CRM_SharePoint_Transports
{
    public partial class SharePointFileTransportConfigControl : UserControl, BlueIntegrator.Extensibility.IBlueIntegratorPropertyPage
    {
        public SharePointFileTransportConfigControl(BlueIntegrator.Messaging.SendPortConfigurationForm spcf)
        {
            InitializeComponent();
            m_spcf = spcf;
            this.Text = "SharePoint";
        }

        /// <summary>
        /// Config click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            SSISLib.SharePointFileConfigForm scf = new SSISLib.SharePointFileConfigForm(m_data, true, m_portId, m_dal);

            if (scf.ShowDialog(this) == DialogResult.OK)
            {
            }
        }

        #region IBlueIntegratorPropertyPage Members

        public void Initialise(System.Collections.IDictionary data)
        {
            m_data = new System.Collections.Hashtable();
            m_origData = new System.Collections.Hashtable();
            this.textBox1.Text = BlueIntegrator.Helpers.Conversion.SafeGetString(data["DestinationURL"]);

            if (data["InitedTitle"] == null)
            {
                data["InitedTitle"] = "1";
                data["NumSPPropertyBindings"] = "1";
                data["SPPropertyBinding:0:DisplayName"] = "Title";
                data["SPPropertyBinding:0:InternalName"] = "Title";
                data["SPPropertyBinding:0:FieldType"] = "Text";
                data["SPPropertyBinding:0:Binding"] = "BlueIntegrator Message %MessageID%";
            }

            foreach (System.Collections.DictionaryEntry dent in data)
            {
                m_data[dent.Key] = dent.Value;
                m_origData[dent.Key] = dent.Value;
            }
            
            BlueIntegrator.DAL.DALBase dal = data["__DAL"] as BlueIntegrator.DAL.DALBase;
            m_dal = dal;
            m_portId = data["__PortId"] as string;

            // populate listview
            for (int i = 0; i < BlueIntegrator.Helpers.Conversion.SafeGetInt(data["NumSPPropertyBindings"]); i++)
            {
                ListViewItem lvi = this.listView1.Items.Add(data["SPPropertyBinding:" + i + ":DisplayName"] as string ?? "?");
                lvi.SubItems.Add (data["SPPropertyBinding:" + i + ":InternalName"] as string ?? "?");
                lvi.SubItems.Add (data["SPPropertyBinding:" + i + ":FieldType"] as string ?? "?");
                lvi.SubItems.Add(data["SPPropertyBinding:" + i + ":Binding"] as string ?? "?");
            }
        }

        public bool ApplyChanges(System.Collections.IDictionary data)
        {
            if (data != m_data)
            foreach (System.Collections.DictionaryEntry dent in m_data)
            {
                if (dent.Value != m_origData[dent.Key] || !data.Contains(dent.Key))
                    data[dent.Key] = dent.Value;
            }

            // reapply bindings...
            data["NumSPPropertyBindings"] = this.listView1.Items.Count;

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                data["SPPropertyBinding:" + i + ":DisplayName"] = this.listView1.Items[i].Text;
                data["SPPropertyBinding:" + i + ":InternalName"] = this.listView1.Items[i].SubItems[1].Text;
                data["SPPropertyBinding:" + i + ":FieldType"] = this.listView1.Items[i].SubItems[2].Text;
                data["SPPropertyBinding:" + i + ":Binding"] = this.listView1.Items[i].SubItems[3].Text;
            }

            data["DestinationURL"] = this.textBox1.Text;

            return true;
        }

        #endregion

        System.Collections.Hashtable m_data, m_origData;
        string m_portId;
        BlueIntegrator.DAL.DALBase m_dal;
        BlueIntegrator.Messaging.SendPortConfigurationForm m_spcf;

        /// <summary>
        /// Property listview sel change handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.button2.Enabled = this.button3.Enabled = this.listView1.SelectedItems.Count == 1;
        }

        /// <summary>
        /// Add handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                SPPropertyForm sp = new SPPropertyForm(null, m_spcf.GetTokens());

                if (sp.ShowDialog(this) == DialogResult.OK)
                {
                    ListViewItem lvi = this.listView1.Items.Add(sp.ResultProperty.DisplayName);
                    lvi.SubItems.Add(sp.ResultProperty.InternalName);
                    lvi.SubItems.Add(sp.ResultProperty.Type.ToString());
                    lvi.SubItems.Add(sp.ResultProperty.Binding);
                }
            }
            catch (System.Exception exc)
            {
                MessageBox.Show("Error : " + exc);
            }
        }

        // Edit handler
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                ListViewItem lvi = this.listView1.SelectedItems[0];
                SPProperty prop = new SPProperty(lvi.Text, lvi.SubItems[1].Text, lvi.SubItems[2].Text, lvi.SubItems[3].Text);
                SPPropertyForm sp = new SPPropertyForm(prop, m_spcf.GetTokens());

                if (sp.ShowDialog(this) == DialogResult.OK)
                {
                    lvi.Text = sp.ResultProperty.DisplayName;
                    lvi.SubItems[1].Text = sp.ResultProperty.InternalName;
                    lvi.SubItems[2].Text = sp.ResultProperty.Type.ToString();
                    lvi.SubItems[3].Text = sp.ResultProperty.Binding;
                }
            }
            catch (System.Exception exc)
            {
                MessageBox.Show("Error : " + exc);
            }
        }

        /// <summary>
        /// Delete handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count == 1)
                this.listView1.Items.Remove(this.listView1.SelectedItems[0]);
        }
    }
}
